﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JewererBasic
{
    public class BagDataSource
    {
        // Bag Infor
        public string BagId { get; set; }
        public int BagQty { get; set; }
        public string BagImage { get; set; }
        public string ListMould { get; set; }
        public string ListStone { get; set; }

        // Product Infor
        public string ProductId { get; set; }
        public string ProductType { get; set; }
        public string ProductGamma { get; set; }
        public string ProductImage { get; set; }
        public string ProductRemark { get; set; }
        

        // Order Infor
        public string OrderId { get; set; }
        public string Customer { get; set; }
        public int FinishWeek { get; set; }

        // Order_Product
        public string ProductKind { get; set; }
        public float ProductSize { get; set; }

        // Helper functions
        public static BagDataSource GetData(string bagId)
        {
            if (string.IsNullOrWhiteSpace(bagId) == false)
            {
                using (MSQDBEntities dc = new MSQDBEntities())
                {
                    var bag = dc.BAGs.Find(bagId);
                    if (bag != null)
                    {
                        ORDER order = bag.ORDER_PRODUCT.ORDER;
                        ORDER_PRODUCT orderProduct= bag.ORDER_PRODUCT;
                        PRODUCT product = orderProduct.PRODUCT;
                        List<BAG_STONE> stones = bag.BAG_STONE.ToList();
                        List<BAG_MOULD> moulds = bag.BAG_MOULD.ToList();

                        BagDataSource result = new BagDataSource();
                        result.BagId = bag.Id;
                        result.BagQty = bag.Number;
                        result.Customer = order.IdCustomer;
                        result.FinishWeek = (int)order.FinishWeek;
                        result.OrderId = order.IdOrder;
                        result.ProductId = product.Id;
                        result.ProductImage = product.Drawing;
                        result.ProductKind = product.KIND.Name;
                        result.ProductRemark = product.Remark;
                        result.ProductSize = (float)product.Size;
                        result.ProductType = product.TYPE.Name;
                        
                        // Get List
                        result.ListMould = "";
                        result.ListStone = "";
                        foreach (var item in stones)
                        {
                            result.ListStone += item.STONE.Id + ":" + item.STONE.Name + ":" +item.Numb +  "\r\n";
                        }
                        foreach (var item in moulds)
                        {
                            result.ListMould += item.MOULD.Name + ":" + item.Numb;
                        }
                        return result;
                    }
                }
            }
            return null;
        }
    }
}